package com.ganyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ganyu.domain.ResponseResult;
import com.ganyu.domain.entity.Menu;
import com.ganyu.domain.vo.AdminMenuTreeVo;
import com.ganyu.domain.vo.AdminMenuUpdateVo;
import com.ganyu.domain.vo.AdminMenuVo;
import com.ganyu.domain.vo.AdminTreeVo;
import com.ganyu.exception.SystemException;
import com.ganyu.mapper.MenuMapper;
import com.ganyu.mapper.RoleMenuMapper;
import com.ganyu.mapper.UserRoleMapper;
import com.ganyu.service.MenuService;
import com.ganyu.utils.BeanCopyUtils;
import com.ganyu.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

import static com.ganyu.constant.SystemConstant.PARENT_ID;
import static com.ganyu.enums.AppHttpCodeEnum.NODELETE;
import static com.ganyu.enums.AppHttpCodeEnum.NOTUPDATE;
import static com.ganyu.utils.SecurityUtils.isAdmin;

/**
 * DATA: 2022/9/28
 * Author: 晨夕
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>  implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    //获取menu集合
    @Override
    public ResponseResult getMenuList(String MenuName,String status) {
        //可以进行模糊查询，不要分页
        List<Menu> menuList = menuMapper.getMenuList(MenuName, status);
        List<AdminMenuVo> adminMenuVos = BeanCopyUtils.copyBeanList(menuList, AdminMenuVo.class);
        return ResponseResult.okResult(adminMenuVos);
    }

    //添加menu
    @Override
    public ResponseResult addMenu(Menu menu) {
        menuService.save(menu);
        return ResponseResult.okResult();
    }

    //修改menu
    @Override
    public ResponseResult updateMenu(Menu menu) {
        //父菜单不能修改为本菜单
        if(menu.getId().equals(menu.getParentId())){
            throw new SystemException(NOTUPDATE);
        }
        menuMapper.updateMenu(menu);
        return ResponseResult.okResult();
    }

    //查找menu
    public ResponseResult selectMenu(Long id) {
        Menu menu = menuMapper.selectById(id);
        AdminMenuUpdateVo adminMenuUpdateVo = BeanCopyUtils.copyBean(menu, AdminMenuUpdateVo.class);
        return ResponseResult.okResult(adminMenuUpdateVo);
    }

    //删除menu
    @Override
    public ResponseResult deleteMenu(Long menuId) {
        //1.查询当前菜单是否有子菜单
        List<Menu> menus = menuMapper.selectChildMenu(menuId);
        if(!menus.isEmpty()){
            throw new SystemException(NODELETE);
        }
        menuMapper.deleteMenu(menuId);
        return ResponseResult.okResult();
    }

    //获取树型表单
    @Override
    public ResponseResult getMenuTree() {
        Long userId = SecurityUtils.getUserId();
        //如果useId为1的话，是管理员，直接获取菜单
        if(isAdmin()){
            //获取为parent_id=0的菜单
            List<Menu> children = menuMapper.getChildren(PARENT_ID);
            List<AdminTreeVo> adminTreeVos = BeanCopyUtils.copyBeanList(children, AdminTreeVo.class);

            //获取子菜单的子菜单
            for(AdminTreeVo adminTree : adminTreeVos){
                List<Menu> childrens = menuMapper.getChildrens(adminTree.getId());
                List<AdminTreeVo> adminTreeVos1 = BeanCopyUtils.copyBeanList(childrens, AdminTreeVo.class);
                adminTree.setChildren(adminTreeVos1);
                adminTree.setLabel(adminTree.getMenuName());
                for(AdminTreeVo adminTree1 : adminTreeVos1){
                    List<Menu> childrens1 = menuMapper.getChildrenList(adminTree1.getId());
                    List<AdminTreeVo> adminTreeVos2 = BeanCopyUtils.copyBeanList(childrens1, AdminTreeVo.class);
                    adminTree1.setChildren(adminTreeVos2);
                    adminTree1.setLabel(adminTree1.getMenuName());
                    for(AdminTreeVo adminTree2 : adminTreeVos2){
                        adminTree2.setLabel(adminTree2.getMenuName());
                    }
                }
            }
            return ResponseResult.okResult(adminTreeVos);
        }
        //根据当前用户id获取其具有的menu
        Long roleId = userRoleMapper.getRoleId(userId);
        List<Long> menuId = roleMenuMapper.getMenuId(roleId);
        ArrayList<AdminTreeVo> TreeVos = new ArrayList<>();
        for(Long id : menuId){
            Menu parentId = menuMapper.getParentId(id);
            AdminTreeVo adminTreeVo = BeanCopyUtils.copyBean(parentId, AdminTreeVo.class);
            TreeVos.add(adminTreeVo);
        }
        //获取子菜单的子菜单
        for(AdminTreeVo adminTree : TreeVos){
            List<Menu> childrens = menuMapper.getChildrens(adminTree.getId());
            List<AdminTreeVo> adminTreeVos1 = BeanCopyUtils.copyBeanList(childrens, AdminTreeVo.class);
            adminTree.setChildren(adminTreeVos1);
            adminTree.setLabel(adminTree.getMenuName());
            for(AdminTreeVo adminTree1 : adminTreeVos1){
                List<Menu> childrens1 = menuMapper.getChildrenList(adminTree1.getId());
                List<AdminTreeVo> adminTreeVos2 = BeanCopyUtils.copyBeanList(childrens1, AdminTreeVo.class);
                adminTree1.setChildren(adminTreeVos2);
                adminTree1.setLabel(adminTree1.getMenuName());
                for(AdminTreeVo adminTree2 : adminTreeVos2){
                    adminTree2.setLabel(adminTree2.getMenuName());
                }
            }
        }
        return ResponseResult.okResult(TreeVos);
    }
    //获取树型表单
    // TODO 完善
    @Override
    public ResponseResult getMenuTree(Long userId) {
        //如果useId为1的话，是管理员，直接获取菜单
        if(userId == 1L){
            //获取为parent_id=0的菜单
            List<Menu> children = menuMapper.getChildren(PARENT_ID);
            List<AdminTreeVo> adminTreeVos = BeanCopyUtils.copyBeanList(children, AdminTreeVo.class);

            //获取子菜单的子菜单
            for(AdminTreeVo adminTree : adminTreeVos){
                List<Menu> childrens = menuMapper.getChildrens(adminTree.getId());
                List<AdminTreeVo> adminTreeVos1 = BeanCopyUtils.copyBeanList(childrens, AdminTreeVo.class);
                adminTree.setChildren(adminTreeVos1);
                adminTree.setLabel(adminTree.getMenuName());
                for(AdminTreeVo adminTree1 : adminTreeVos1){
                    List<Menu> childrens1 = menuMapper.getChildrenList(adminTree1.getId());
                    List<AdminTreeVo> adminTreeVos2 = BeanCopyUtils.copyBeanList(childrens1, AdminTreeVo.class);
                    adminTree1.setChildren(adminTreeVos2);
                    adminTree1.setLabel(adminTree1.getMenuName());
                    for(AdminTreeVo adminTree2 : adminTreeVos2){
                        adminTree2.setLabel(adminTree2.getMenuName());
                    }
                }
            }
            //获取当前用户的所有menu_id
            List<Long> menuId = roleMenuMapper.getMenuId(userId);
            AdminMenuTreeVo adminMenuTreeVo = new AdminMenuTreeVo();
            adminMenuTreeVo.setMenus(adminTreeVos);
            adminMenuTreeVo.setCheckedKeys(menuId);
            return ResponseResult.okResult(adminMenuTreeVo);
        }
        //根据当前用户id获取其具有的menu
        Long roleId = userRoleMapper.getRoleId(userId);
        List<Long> menuId = roleMenuMapper.getMenuId(roleId);
        ArrayList<AdminTreeVo> TreeVos = new ArrayList<>();
        for(Long id : menuId){
            Menu menu = menuMapper.selectMenu(id);
            AdminTreeVo adminTreeVo1 = BeanCopyUtils.copyBean(menu, AdminTreeVo.class);
            TreeVos.add(adminTreeVo1);
        }
        //获取子菜单的子菜单
        for(AdminTreeVo adminTree : TreeVos){
            List<Menu> childrens = menuMapper.getChildrens(adminTree.getId());
            List<AdminTreeVo> adminTreeVos1 = BeanCopyUtils.copyBeanList(childrens, AdminTreeVo.class);
            adminTree.setChildren(adminTreeVos1);
            adminTree.setLabel(adminTree.getMenuName());
            for(AdminTreeVo adminTree1 : adminTreeVos1){
                List<Menu> childrens1 = menuMapper.getChildrenList(adminTree1.getId());
                List<AdminTreeVo> adminTreeVos2 = BeanCopyUtils.copyBeanList(childrens1, AdminTreeVo.class);
                adminTree1.setChildren(adminTreeVos2);
                adminTree1.setLabel(adminTree1.getMenuName());
                for(AdminTreeVo adminTree2 : adminTreeVos2){
                    adminTree2.setLabel(adminTree2.getMenuName());
                }
            }
        }
        List<Long> id = roleMenuMapper.getMenuId(userId);
        AdminMenuTreeVo adminMenuTreeVo = new AdminMenuTreeVo();
        adminMenuTreeVo.setMenus(TreeVos);
        adminMenuTreeVo.setCheckedKeys(id);
        return ResponseResult.okResult(adminMenuTreeVo);
    }

}
